
TOOL.Category		= "Wire - Physics"
TOOL.Name			= "Weight Modifier (Wired)"
TOOL.Command		= nil
TOOL.ConfigName		= ""

if CLIENT then
	
    language.Add( "Tool_wire_weightmod_name", "Weight Modifier" )
    language.Add( "Tool_wire_weightmod_desc", "Makes a controllable weight modifier on a selected prop" )
    language.Add( "Tool_wire_weightmod_0", "Primary: Click on the entity to be modified" )
    language.Add( "Tool_wire_weightmod_1", "Left click to place the controller" )
	language.Add( "undone_wireweightmod", "Undone Wire Weight Modifier" )
	
end

function TOOL:LeftClick( trace )
	
	if ( trace.Entity:IsValid() && trace.Entity:IsPlayer() ) then return end
	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
	local iNum = self:NumObjects()
		self:SetObject( iNum + 1, trace.Entity, trace.HitPos, Phys, trace.PhysicsBone, trace.HitNormal )
	
	if ( iNum == 1 ) then
			
		if ( CLIENT ) then
			self:ClearObjects()
			return true
		end
		
		local ply = self:GetOwner()
		local Ent1 = self:GetEnt(1)
		local Ang = trace.HitNormal:Angle()
		Ang.pitch = Ang.pitch + 90
		local controller = MakeWireWeightmodController( ply, trace.HitPos, Ang )
		controller:SendVars(Ent1)
		local min = controller:OBBMins()
		controller:SetPos( trace.HitPos - trace.HitNormal * min.z )		
		undo.Create("WireWeightmod")
		undo.AddEntity( controller )
		undo.SetPlayer( ply )
		undo.Finish()		
		self:ClearObjects()
		self:SetStage( 0 )
		
	else
		self:SetStage( iNum + 1 )		
	end

	return true
	
end

if SERVER then
	
	function MakeWireWeightmodController( pl, Pos, Ang )
		local controller = ents.Create("gmod_wire_weightmod")
		
		controller:SetPos( Pos )
		controller:SetAngles( Ang )
		controller:SetPlayer(pl)
		
		controller:Spawn()
		
		return controller
	end
	duplicator.RegisterEntityClass("gmod_wire_weightmod", MakeWireWeightmodController, "Pos", "Ang")
		
end

function TOOL.BuildCPanel( panel )
	panel:AddControl( "Header", { Text = "#Tool_wire_weightmod_name", Description = "#Tool_wire_weightmod_desc" } )
end
